﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DLL;
using System.Data.Linq;
using System.Data;


namespace BLL
{
    public class SanPhamGiamGiaBLL
    {
         luuliDataContext DB = new luuliDataContext();
        public DataTable laydiadiem()
    {
        DataTable b = LinqToDataTable(DB.diadiemgiamgias);
        return b;
    }

        public String tencty_theoIDsp(int id)
        {
            DataTable dd = LinqToDataTable(DB.sanphamgiamgias.Where(q => q.Id_sp == id));
            int Id = int.Parse(dd.Rows[0][10].ToString());

            DataTable cty = LinqToDataTable(DB.congties.Where(q => q.Id_cty == Id));
            String ten = cty.Rows[0][1].ToString();
            return ten;
        }

        public String tensploai2_theoIDsp(int id)
        {
            DataTable dd = LinqToDataTable(DB.sanphamgiamgias.Where(q => q.Id_sp == id));
            String Id = dd.Rows[0][11].ToString();

            DataTable sploai2 = LinqToDataTable(DB.sanpham_loai2s.Where(q => q.Id_sploai2 == Id));
            String ten = sploai2.Rows[0][1].ToString();
            return ten;
        }

        public String tentloai_theoIDsp(int id)
        {
            DataTable dd = LinqToDataTable(DB.sanphamgiamgias.Where(q => q.Id_sp == id));
            int Id = int.Parse(dd.Rows[0][12].ToString());

            DataTable cty = LinqToDataTable(DB.theloais.Where(q => q.Id_tloai == Id));
            String ten = cty.Rows[0][1].ToString();
            return ten;
        }
        //lay sp giam gia nhieu nhat
        public DataTable LayGiamGiaNhieuNhat()
        {
            DataTable a = LinqToDataTable(DB.sanphamgiamgias.OrderByDescending(q => q.Phantramgiam_sp).Take(3));
            return a;

        }
        //lay phan tram giam gia sp
        public DataTable LaySPGiamGia(int phantramgiamgia)
        {
            DataTable spgiamgia = LinqToDataTable(DB.sanphamgiamgias.Where(p => p.Phantramgiam_sp >= phantramgiamgia).OrderByDescending(q => q.Phantramgiam_sp).Take(3));
            return spgiamgia;
        }
        //lay tat ca
        public  DataTable laytatca()
        {
            DataTable a = LinqToDataTable(DB.sanphamgiamgias.Take(12).OrderByDescending(q => q.Id_sp));
            return a;
        }

        //lay sp giam gia moi nhat
        public DataTable LaySPnew()
        {
            DataTable a = LinqToDataTable(DB.sanphamgiamgias.Where(q => q.Ngaytao_sp > DateTime.Now).Take(10).OrderByDescending(q => q.Ngaytao_sp));
            return a;
        }
        //tra ve datatalbe
        public DataTable LinqToDataTable<T>(System.Collections.Generic.IEnumerable<T> varlist)
        {
            try
            {
                System.Data.DataTable dtReturn = new System.Data.DataTable();
                System.Reflection.PropertyInfo[] oProps = null;
                if (varlist == null) return dtReturn;
                foreach (T rec in varlist)
                {
                    if (oProps == null)
                    {
                        oProps = ((Type)rec.GetType()).GetProperties();
                        foreach (System.Reflection.PropertyInfo pi in oProps)
                        {
                            Type colType = pi.PropertyType;
                            if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()== typeof(Nullable<>)))
                            {
                                colType = colType.GetGenericArguments()[0];
                            }
                        dtReturn.Columns.Add(new System.Data.DataColumn(pi.Name, colType));
                        }
                    }
                    System.Data.DataRow dr = dtReturn.NewRow();
                    foreach (System.Reflection.PropertyInfo pi in oProps)
                    {
                        dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value :pi.GetValue(rec, null);
                    }
                    dtReturn.Rows.Add(dr);
                    }
                return dtReturn;
                }
                  catch (Exception ex) { return null; }
                }
        //lay 1 san pham
        public DataTable selectdetail(int id)
        {
            DataTable a = LinqToDataTable(DB.sanphamgiamgias.Where(q => q.Id_sp == id));
            return a;
        }
      
    }
       
}
